package com.irdstudio.efp.esb.api.service.yed;

import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.esb.api.rest.yed.resp.AccLoanArr;
import com.irdstudio.efp.esb.api.rest.yed.resp.RepayDetailArr;
import com.irdstudio.efp.esb.api.service.sed.ploaninfo.PloaninfoUtils;
import com.irdstudio.efp.esb.service.bo.resp.hj.HjRpayDetaillInfArry;
import com.irdstudio.efp.esb.service.bo.resp.hj.RepayInfoQueryRespBean;
import com.irdstudio.efp.loan.service.vo.AccLoanVO;
import com.irdstudio.efp.loan.service.vo.LoanRepayDetailVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 需求编号：xxxxxx<br/>
 * 问题编号：<br/>
 * 功能描述：优e贷还款明细数据封装<br/>
 * 创建时间：2020/9/16 18:29
 * @Author tiankg
 * @Version 1.0
 * @Since 1.0
 */
@Service
public class YedRepayDetailService {

    /**
     * 设置还款明细的数据
     */
    private RepayDetailArr c2DetailArr(LoanRepayDetailVO loanRepayDetailVO){
        RepayDetailArr repayDetailArr = new RepayDetailArr();
        //还款申请流水号
        repayDetailArr.setRpymtAppSrlNo(loanRepayDetailVO.getSetlSeq());
        //剩余本金
        // repayDetailArr.setRmanPnp(loanRepayDetailVO.getPsRemPrcp());
        //还款期数
        repayDetailArr.setRpymtTrmCnt(loanRepayDetailVO.getRepayTerm());
        //还款类型
        repayDetailArr.setRpymtTp(loanRepayDetailVO.getSetlTyp());
        //执行利率
//        repayDetailArr.setExecIntRt(loanRepayDetailVO.getIntRat());
        //还款总计金额
        repayDetailArr.setRpymtTotAmt(loanRepayDetailVO.getTotalAmt());
        //归还本金
        repayDetailArr.setRpyblPnp(loanRepayDetailVO.getSetlOdPrcpAmt());
        //归还利息
        repayDetailArr.setRpyblInt(loanRepayDetailVO.getSetlPsIncTaken());
        //归还罚息
        repayDetailArr.setRpyblPnyInt(loanRepayDetailVO.getSetlOdIntAmt());
        //归还复利
        repayDetailArr.setRpyblCmpdInt(loanRepayDetailVO.getSetlOdCommInt());
        //归还复利的复利
        repayDetailArr.setRpyCmpdIntPrsnlCmpdInt(BigDecimal.ZERO);
        //归还罚息的复利
        repayDetailArr.setRpyPnyIntPrsnlCmpdInt(BigDecimal.ZERO);
        //归还费用
        repayDetailArr.setRpyblFee(loanRepayDetailVO.getSetlTotalFee());
        //还款账号
        repayDetailArr.setRpymtAcctNo(loanRepayDetailVO.getDisbAcNo());
        //还款状态
        repayDetailArr.setRpymtSt(loanRepayDetailVO.getRpSts());
        //还款日期
        repayDetailArr.setRpymtDt(loanRepayDetailVO.getSetlApplyDt());
        //审批状态wfApprSts
        repayDetailArr.setAprvlSt(loanRepayDetailVO.getWfApprSts());
        //失败原因refuseCause
        repayDetailArr.setFailRsn(loanRepayDetailVO.getRefuseCause());
        return repayDetailArr;
    }

    private RepayDetailArr c2DetailArr(HjRpayDetaillInfArry rpyDtlInfArry, List<LoanRepayDetailVO> loanRepayDetails) {
        RepayDetailArr repayDetailArr = new RepayDetailArr();
        //还款申请流水号
        String setlSeq = "";
        if (loanRepayDetails != null) {
            for (LoanRepayDetailVO loanRepayDetail : loanRepayDetails) {
                if (rpyDtlInfArry.getRpyNo().equals(loanRepayDetail.getRpyNo())) {
                    setlSeq = loanRepayDetail.getSetlSeq();
                    repayDetailArr.setRpymtTrmCnt(loanRepayDetail.getRepayTerm());//还款期数
                    break;
                }
            }
        }
        repayDetailArr.setRpymtAppSrlNo(StringUtils.isEmpty(setlSeq) ? rpyDtlInfArry.getRpyNo() : setlSeq);
       /* 网贷还款类型	    互金还款类型
        01 正常还款	        04-NS-正常
        02 提前部分还款	    02-ER-提前部分还本
        03 提前结清	        01-PO-全部结清
        04 逾期还款	        06-NSYUQ-逾期还款
        06 按期次提前还款	    05-PS-按期次提前还款
        10 部分还款（先还欠款如有剩余提前还本）	03-PR-部分还款
        11逾期结清           07-POYUQ po逾期结清
        09息费减免           09-WV息费减免*/
        if (StringUtils.isNotEmpty(rpyDtlInfArry.getRpyType())) {
            if (ConsoleConstant.REPAY_TYPE_01.equals(rpyDtlInfArry.getRpyType())) {
                repayDetailArr.setRpymtTp(ConsoleConstant.REPAY_TYPE_03);
            }
            if (ConsoleConstant.REPAY_TYPE_02.equals(rpyDtlInfArry.getRpyType())) {
                repayDetailArr.setRpymtTp(ConsoleConstant.REPAY_TYPE_02);
            }
            if (ConsoleConstant.REPAY_TYPE_03.equals(rpyDtlInfArry.getRpyType())) {
                repayDetailArr.setRpymtTp(ConsoleConstant.REPAY_TYPE_10);
            }
            if (ConsoleConstant.REPAY_TYPE_04.equals(rpyDtlInfArry.getRpyType())) {
                repayDetailArr.setRpymtTp(ConsoleConstant.REPAY_TYPE_01);
            }
            if (ConsoleConstant.REPAY_TYPE_05.equals(rpyDtlInfArry.getRpyType())) {
                repayDetailArr.setRpymtTp(ConsoleConstant.REPAY_TYPE_06);
            }
            if (ConsoleConstant.REPAY_TYPE_06.equals(rpyDtlInfArry.getRpyType())) {
                repayDetailArr.setRpymtTp(ConsoleConstant.REPAY_TYPE_04);
            }
            if (ConsoleConstant.REPAY_TYPE_07.equals(rpyDtlInfArry.getRpyType())) {
                repayDetailArr.setRpymtTp(ConsoleConstant.REPAY_TYPE_11);
            }
            if (ConsoleConstant.REPAY_TYPE_09.equals(rpyDtlInfArry.getRpyType())) {
                repayDetailArr.setRpymtTp(ConsoleConstant.REPAY_TYPE_09);
            }
        }
        //执行利率
//        repayDetailArr.setExecIntRt();
        //还款总计金额
        repayDetailArr.setRpymtTotAmt(rpyDtlInfArry.getRpyAmt());
        //归还本金
        repayDetailArr.setRpyblPnp(rpyDtlInfArry.getRcylPnpAmt());
        //归还利息
        repayDetailArr.setRpyblInt(rpyDtlInfArry.getRcylIntAmt());
        //归还罚息
        repayDetailArr.setRpyblPnyInt(rpyDtlInfArry.getRcylPnyIntAmt());
        //归还复利
        repayDetailArr.setRpyblCmpdInt(rpyDtlInfArry.getRcylCmpdIntAmt());
        //归还复利的复利
        repayDetailArr.setRpyCmpdIntPrsnlCmpdInt(rpyDtlInfArry.getRcylCmpdIntNrlCmpdIntAmt());
        //归还罚息的复利
        repayDetailArr.setRpyPnyIntPrsnlCmpdInt(rpyDtlInfArry.getRcylPnyIntNrlCmpdIntAmt());
        //归还费用
        repayDetailArr.setRpyblFee(PloaninfoUtils.getAddResult(rpyDtlInfArry.getRcylPnpAmt(), rpyDtlInfArry.getRcylIntAmt(), rpyDtlInfArry.getRcylCmpdIntAmt(), rpyDtlInfArry.getRcylPnyIntAmt(), rpyDtlInfArry.getRcylCmpdIntNrlCmpdIntAmt(), rpyDtlInfArry.getRcylPnyIntNrlCmpdIntAmt()));
        //还款账号
        repayDetailArr.setRpymtAcctNo("");
        //还款状态(互金无返回)
        repayDetailArr.setRpymtSt(rpyDtlInfArry.getRpyAmt().doubleValue() <= 0 ? "0" : "1");
        //还款日期
        repayDetailArr.setRpymtDt(LocalDate.parse(rpyDtlInfArry.getRpyDt(), DateTimeFormatter.ofPattern("yyyyMMdd")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        //审批状态wfApprSts(互金无返回)
        repayDetailArr.setAprvlSt(rpyDtlInfArry.getRpyAmt().doubleValue() <= 0 ? "03" : "02");
        //失败原因refuseCause
        repayDetailArr.setFailRsn(rpyDtlInfArry.getErrDescInf());
        //应还日期
        repayDetailArr.setRpyblPymtDt(rpyDtlInfArry.getRpyblPymtDt());
        return repayDetailArr;
    }

    /**
     * @param loanRepayDetailVOS      本地的还款记录数据
     * @param accLoanVO               本地的借据信喜
     * @param repayInfoQueryRespBean  根据贷款发放号查询互金返回的贷款回收信息
     */
    public void yedhandleResp(List<AccLoanArr> accLoanArrs,ArrayList<RepayDetailArr> repayDetailArrs,List<LoanRepayDetailVO> loanRepayDetailVOS, AccLoanVO accLoanVO, RepayInfoQueryRespBean repayInfoQueryRespBean) {
        //从本地取失败的记录，输出
        List<LoanRepayDetailVO> localFails = loanRepayDetailVOS.stream().filter(r -> !"02".equals(r.getWfApprSts())).collect(Collectors.toList());
        if (localFails != null && localFails.size() >= 1) {
            localFails.stream().forEach(r -> repayDetailArrs.add(c2DetailArr(r)));
        }

        //取互金回收成功的记录输出
        ArrayList<HjRpayDetaillInfArry> rpyDtlInfArry = repayInfoQueryRespBean.getRpyDtlInfArrys();
        if (rpyDtlInfArry != null && rpyDtlInfArry.size() > 0) {
            for (HjRpayDetaillInfArry hjRpayDetaillInfArry : rpyDtlInfArry) {
                if (hjRpayDetaillInfArry.getRpyAmt().doubleValue() > 0) {
                    repayDetailArrs.add(c2DetailArr(hjRpayDetaillInfArry, loanRepayDetailVOS));
                }
            }
        }
        AccLoanArr accLoanArr = AccLoanArr.builder()
                .billNo(accLoanVO.getBillNo())
                .certType(accLoanVO.getCertType())
                .certCode(accLoanVO.getCertCode())
                .cusName(accLoanVO.getCusName())
                .dnAmt(accLoanVO.getLoanAmount())
                .repayDetailArr(repayDetailArrs).build();
        accLoanArrs.add(accLoanArr);
    }
}
